package cn.codesea.housework_user.ui.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.HomeRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickActivityListener;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import cn.codesea.housework_user.ui.AcActivity;
import cn.codesea.housework_user.ui.AcListActivity;
import cn.codesea.housework_user.ui.ServiceListActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Tab1ActManagerFragment extends Fragment{


    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    List<Activity> acts = new ArrayList<>();
    private HomeRcvAdapter mHomeRcvAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @BindView(R.id.add_ll)
    LinearLayout add_ll;

    @BindView(R.id.order_waiting_ll)
    LinearLayout order_waiting_ll;// 已开始
    @BindView(R.id.order_waiting_tv)
    TextView order_waiting_tv;
    @BindView(R.id.order_waiting_iv)
    ImageView order_waiting_iv;
    @BindView(R.id.order_received_ll)
    LinearLayout order_received_ll;// 未开始
    @BindView(R.id.order_received_tv)
    TextView order_received_tv;
    @BindView(R.id.order_received_iv)
    ImageView order_received_iv;

    int index = 0;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1_manager_activity,container,false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            updateRecyclerView(index);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateRecyclerView(index);
    }

    private void init(){
        updateRecyclerView(index);
        initView();
        setTab(0);
    }

    private void initView(){

        mHomeRcvAdapter = new HomeRcvAdapter(getActivity(),acts);
        mHomeRcvAdapter.setRecyclerItemClickListener(new OnRecyclerItemClickActivityListener() {
            @Override
            public void onItemClick(int Position, List<Activity> list) {

                if (index == 0){
                    startActivity(new Intent(getActivity(),AcEditActivity.class).putExtra("act",acts.get(Position)));
                }

            }
        });
        mLinearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mHomeRcvAdapter);

    }

    /**
     * tab页选择
     */
    private void setTab(int index){
        this.index = index;
        this.acts.clear();// 清除数据
        mHomeRcvAdapter.notifyDataSetChanged();
        selChangeBg(index);// 处理背景
        updateRecyclerView(index);// 更新数据
    }

    private void updateRecyclerView(int index) {
        ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = activityService.getActs(index);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    private void resultProcess(Result result){
        Type type = new TypeToken<List<Activity>>(){}.getType();
        acts.clear();
        acts.addAll((List<Activity>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        mHomeRcvAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.add_ll)
    public void add_ll(){
        startActivity(new Intent(getActivity(),AcEditActivity.class).putExtra("type","1"));
    }
    @OnClick(R.id.order_waiting_ll)
    public void order_waiting_ll(){// 待接单
        setTab(0);
    }

    @OnClick(R.id.order_received_ll)
    public void order_received_ll(){// 已接单
        setTab(1);
    }


    /**
     * 选择tab
     * @param index
     */
    private void selChangeBg(int index){
        switch (index){
            case 0:
                order_waiting_ll.setBackgroundColor(getResources().getColor(R.color.primary));
                order_waiting_tv.setTextColor(getResources().getColor(R.color.white));
                order_waiting_iv.setImageResource(R.drawable.tab_1_white);

                order_received_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_received_tv.setTextColor(getResources().getColor(R.color.black));
                order_received_iv.setImageResource(R.drawable.tab_2_red);

                break;
            case 1:
                order_waiting_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_waiting_tv.setTextColor(getResources().getColor(R.color.black));
                order_waiting_iv.setImageResource(R.drawable.tab_1_red);

                order_received_ll.setBackgroundColor(getResources().getColor(R.color.primary));
                order_received_tv.setTextColor(getResources().getColor(R.color.white));
                order_received_iv.setImageResource(R.drawable.tab_2_white);

                break;
            case 2:
                order_waiting_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_waiting_tv.setTextColor(getResources().getColor(R.color.black));
                order_waiting_iv.setImageResource(R.drawable.tab_1_red);

                order_received_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_received_tv.setTextColor(getResources().getColor(R.color.black));
                order_received_iv.setImageResource(R.drawable.tab_2_red);

                break;
            case 3:
                order_waiting_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_waiting_tv.setTextColor(getResources().getColor(R.color.black));
                order_waiting_iv.setImageResource(R.drawable.tab_1_red);

                order_received_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_received_tv.setTextColor(getResources().getColor(R.color.black));
                order_received_iv.setImageResource(R.drawable.tab_2_red);

                break;
            default:
                order_waiting_ll.setBackgroundColor(getResources().getColor(R.color.primary));
                order_waiting_tv.setTextColor(getResources().getColor(R.color.white));
                order_waiting_iv.setImageResource(R.drawable.tab_1_white);

                order_received_ll.setBackgroundColor(getResources().getColor(R.color.white));
                order_received_tv.setTextColor(getResources().getColor(R.color.black));
                order_received_iv.setImageResource(R.drawable.tab_2_red);

                break;
        }
    }

}
